<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.inquiry.template.form') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('business.inquiry.template.master_chinese_content')" prop="master_chinese_content">
            <el-input type="textarea" :placeholder="i18n('business.inquiry.template.master_chinese_content')" v-model="dataForm.master_chinese_content" show-word-limit maxlength="50"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('business.inquiry.template.master_english_content')" prop="master_english_content">
            <el-input type="textarea" :placeholder="i18n('business.inquiry.template.master_english_content')" v-model="dataForm.master_english_content" show-word-limit maxlength="100"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('business.inquiry.template.slave_chinese_content')" prop="slave_chinese_content">
            <el-input type="textarea" :placeholder="i18n('business.inquiry.template.slave_chinese_content')" v-model="dataForm.slave_chinese_content" show-word-limit maxlength="50"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('business.inquiry.template.slave_english_content')" prop="slave_english_content">
            <el-input type="textarea" :placeholder="i18n('business.inquiry.template.slave_english_content')" v-model="dataForm.slave_english_content" show-word-limit maxlength="100"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('module:business:inquiry:template:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-inquiry-template-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/inquiry/template')

  const dataForm = reactive({
    id: 0,
    master_chinese_content: '',
    master_english_content: '',
    slave_chinese_content: '',
    slave_english_content: '',
  })

  const dataRule = reactive({})

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.master_chinese_content = data.data.master_chinese_content
            dataForm.master_english_content = data.data.master_english_content
            dataForm.slave_chinese_content = data.data.slave_chinese_content
            dataForm.slave_english_content = data.data.slave_english_content
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
